import pymysql

from conn_mysql import ConnectMysql

import datetime

class ApiCurrent(ConnectMysql):

    def open_id(self,openid):
        '''Look up all the information in the openid table,
        parameter: the user corresponds to openid'''

        sql = 'select * from openid where openid = %s'

        result_info = self.select_one(sql, (openid,))

        return result_info

    def company_info(self, company_id):
        '''Present the data in the company table'''

        sql = 'select * from company where id = %s'

        result_info = self.select_one(sql, (company_id,))

        return result_info


    def current_openid(self, methods, openid):
        '''Implement functions with all the information in the openid table, parameter: user corresponds to openid'''

        if not methods == 'POST':
    
            return {
                'status': -1,
                'errorCode': 4,
                'errorMsg':f'请检查request.method:{methods}请重新请求',
                'resultBody':None
            }

        if not openid:

            return {
                'status': -1,
                'errorCode': 1,
                'errorMsg':f'请检查参数是否为空,openid:{openid},请重新输入',
                'resultBody':None
            }

        openid_table_info = self.open_id(openid)

        if not openid_table_info:

            return {
                'status': -1,
                'errorCode': 2,
                'errorMsg':f'没有此,openid:{openid},请重新输入',
                'resultBody':None
            }

        else:

            company_id = openid_table_info['company_id']

            pay = openid_table_info['pay']

            if company_id:

                company_update = self.company_info(company_id)['update_time']

                company_state = 1

                now = datetime.datetime.now()

                time_line = datetime.timedelta(seconds=3600*(24-8))

                if now-company_update > time_line:

                    company_update = 1

                    return {
                        'status': 1,
                        'errorCode': None,
                        'errorMsg':None,
                        'resultBody':{'openid_state':{'company_state':company_state,'company_id':company_id, 'pay':pay, 'company_update':company_update}}
                    }

                else:

                    company_update = 0

                    return {
                        'status': 1,
                        'errorCode': None,
                        'errorMsg':None,
                        'resultBody':{'openid_state':{'company_state':company_state, 'pay':pay, 'company_update':company_update}}
                    }
            
            else:

                company_state = 0 

                company_update = 0

                return {
                    'status': 1,
                    'errorCode': None,
                    'errorMsg':None,
                    'resultBody':{'openid_state':{'company_state':company_state, 'pay':pay, 'company_update':company_update}}
                }

        

    